<template>
  <div class="app-container">
    <div v-if="step==1">
      <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
        <el-form-item label="套件名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入套件名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['admin:assets:kits:post']"
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['admin:assets:kits:put']"
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['admin:assets:kits:delete']"
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            v-hasPermi="['admin:assets:assets:export']"-->
<!--            type="primary"-->
<!--            plain-->
<!--            icon="el-icon-position"-->
<!--            size="mini"-->
<!--            :loading="exportLoading"-->
<!--            @click="handleInput"-->
<!--          >导入</el-button>-->
<!--        </el-col>-->
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            v-hasPermi="['admin:assets:kits:export']"-->
<!--            type="warning"-->
<!--            plain-->
<!--            icon="el-icon-download"-->
<!--            size="mini"-->
<!--            :loading="exportLoading"-->
<!--            @click="handleExport"-->
<!--          >导出</el-button>-->
<!--        </el-col>-->
        <right-toolbar :show-search.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="kitsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!--        <el-table-column label="ID" width="100" align="center" prop="id" />-->
        <el-table-column label="配件名称" align="left" prop="name">
          <template slot-scope="scope">
            <el-link type="primary" @click="clickAKit(scope)">{{ scope.row.name }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="借出操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="primary"
              @click="handleCheckout(scope.row)"
            >借出</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['admin:assets:kits:put']"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              v-hasPermi="['admin:assets:kits:delete']"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

    </div>

    <div v-if="step==2">
      <detail :top-id="topId" @fanhui="fanhui"></detail>
    </div>
    <!-- 添加或修改附属品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="套件名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入套件名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <Borrow ref="borrow" :borrow-show="itemChoose" asset-type="kits" @commitData="submitFormOutIn"></Borrow>
  </div>
</template>

<script>
import { listKits, getKits, delKits, addKits, updateKits, exportKits } from "@/api/vadmin/assets/kits";
import detail from "./detail.vue";
import Borrow from "@/views/components/Borrow";

export default {
  name: "Accessories",
  components: { detail, Borrow },
  data() {
    return {
      itemChoose: null,
      topId: 0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 附属品表格数据
      kitsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "套件名称不能为空", trigger: "blur" }
        ]
      },
      step: 1
    };
  },
  created() {
    this.getList();
  },
  methods: {
    submitFormOutIn() {
      this.$message({
        message: "借出成功",
        type: "success"
      });
      this.$refs.borrow.close();
      this.getList();
    },
    fanhui() {
      this.step = 1;
    },
    /** 查询附属品列表 */
    getList() {
      this.loading = true;
      listKits(this.queryParams).then(response => {
        response = response.data;
        this.kitsList = response.results;
        this.total = response.count;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        description: null,
        modifier: null,
        dept_belong_id: null,
        update_datetime: null,
        create_datetime: null,
        name: null,
        deleted_datetime: null,
        qty: null,
        requestable: null,
        purchase_date: null,
        purchase_cost: null,
        order_number: null,
        min_amt: null,
        model_number: null,
        image: null,
        notes: null,
        category_id: null,
        creator_id: null,
        manufacturer_id: null,
        supplier_id: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加套件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getKits(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改套件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateKits(this.form).then(response => {
              this.$message({ type: "info", message: "修改成功" });
              this.open = false;
              this.getList();
            });
          } else {
            addKits(this.form).then(response => {
              this.$message({ type: "info", message: "新增成功" });
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除资产套件编号为"' + ids + '"的数据项？').then(function() {
        return delKits(ids);
      }).then(() => {
        this.getList();
        this.$message({ type: "info", message: "删除成功" });
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm("是否确认导出所有附属品数据项？").then(() => {
        this.exportLoading = true;
        return exportKits(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    handleCheckout(val) {
      this.itemChoose = val;
      this.$refs.borrow.$nextTick(() => {
        this.$refs.borrow.open();
      });
    },
    handleCheckin() {
      alert("暂未实现");
    },
    clickAKit(scope) {
      this.topId = scope.row.id;
      this.step = 2;
    }
  }
};
</script>
